package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.vo.SysApplyExportVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPositionsApply;
import com.ruoyi.system.service.ISysPositionsApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报名管理信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/system/apply")
public class SysPositionsApplyController extends BaseController
{
    @Autowired
    private ISysPositionsApplyService sysPositionsApplyService;

    /**
     * 查询报名管理信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPositionsApply sysPositionsApply)
    {
        startPage();
        List<SysPositionsApply> list = sysPositionsApplyService.selectSysPositionsApplyList(sysPositionsApply);
        return getDataTable(list);
    }

    /**
     * 获取近一周报名数据
     */
    @PreAuthorize("@ss.hasPermi('system:apply:list')")
    @GetMapping("/weeklyApplyData")
    public TableDataInfo getWeeklyApplyData(SysPositionsApply sysPositionsApply) {
        List<SysPositionsApply> list = sysPositionsApplyService.selectWeeklyApplyData(sysPositionsApply);
        return getDataTable(list);
    }



    /**
     * 导出报名管理信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:apply:export')")
    @Log(title = "报名管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPositionsApply sysPositionsApply)
    {
        List<SysPositionsApply> list = sysPositionsApplyService.selectSysPositionsApplyList(sysPositionsApply);
        ExcelUtil<SysPositionsApply> util = new ExcelUtil<SysPositionsApply>(SysPositionsApply.class);
        util.exportExcel(response, list, "报名管理信息数据");
    }

    /**
     * 导出所有报名数据（包含用户信息和岗位信息）
     */
    @PreAuthorize("@ss.hasPermi('system:apply:export')")
    @Log(title = "报名管理信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export/all")
    public void exportAll(HttpServletResponse response, SysPositionsApply sysPositionsApply) {
        List<SysApplyExportVO> list = sysPositionsApplyService.selectSysPositionsApplyExportList(sysPositionsApply);
        ExcelUtil<SysApplyExportVO> util = new ExcelUtil<SysApplyExportVO>(SysApplyExportVO.class);
        util.exportExcel(response, list, "报名管理信息数据");
    }


    /**
     * 获取报名管理信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:apply:query')")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") Long applyId)
    {
        return success(sysPositionsApplyService.selectSysPositionsApplyByApplyId(applyId));
    }

    /**
     * 获取个人报名管理信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:apply:query')")
    @PostMapping(value = "/getInfo/")
    public AjaxResult getInfo(@RequestBody SysPositionsApply sysPositionsApply) {
        String idcard = sysPositionsApply.getIdcard();
        String batchNumber = sysPositionsApply.getBatchNumber();
        return success(sysPositionsApplyService.selectSysPositionsApplyByidcardAndBatchNumber(sysPositionsApply));
    }





    /**
     * 新增报名管理信息
     */
//    @PreAuthorize("@ss.hasPermi('system:apply:add')")
    @Log(title = "报名管理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPositionsApply sysPositionsApply)
    {
        return toAjax(sysPositionsApplyService.insertSysPositionsApply(sysPositionsApply));
    }

    /**
     * 修改报名管理信息
     */
    @PreAuthorize("@ss.hasPermi('system:apply:edit')")
    @Log(title = "报名管理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPositionsApply sysPositionsApply)
    {
        return toAjax(sysPositionsApplyService.updateSysPositionsApply(sysPositionsApply));
    }

    /**
     * 删除报名管理信息
     */
    @PreAuthorize("@ss.hasPermi('system:apply:remove')")
    @Log(title = "报名管理信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable Long[] applyIds)
    {
        return toAjax(sysPositionsApplyService.deleteSysPositionsApplyByApplyIds(applyIds));
    }

    /**
     * 删除报名管理信息
     */
//    @PreAuthorize("@ss.hasPermi('system:apply:remove')")
    @Log(title = "报名管理信息")
    @GetMapping(value = "/delete/{applyId}")
    public AjaxResult remove(@PathVariable Long applyId)
    {
        return sysPositionsApplyService.deleteSysPositionsApplyByApplyId(applyId);
    }

    @GetMapping("/deletebyProcessId/{processId}")
    public AjaxResult deleteApplyByProcessId(@PathVariable String processId) {
        int rows = sysPositionsApplyService.deleteSysPositionsApplyByProcessId(processId);
        return AjaxResult.success(rows > 0 ? "删除成功" : "未找到匹配记录");
    }

    /**
     * 查看当前用户是否具有报名权限
     */
    @GetMapping("/haspermit")
    public AjaxResult checkApplyPermission() {
        int hasPermission = sysPositionsApplyService.checkApplyPermission();
        switch (hasPermission) {
            case 0:
                return AjaxResult.success("权限通过", 0);
            case 1:
                return AjaxResult.error("用户未登录");
            case 2:
                return AjaxResult.success("用户没有报名记录", 2);
            case 3:
                return AjaxResult.success("报名记录超过5条",3);
            case 4:
                return AjaxResult.success("存在待审批的报名申请",4);
            case 5:
                return AjaxResult.success("存在已通过的申请",5);
            default:
                return AjaxResult.error("未知错误");
        }
    }


}
